﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace DAL
{
    public delegate void AddMainLogDelegate(string log);
    public delegate void OrderStateDelegate(int State);

    #region 单例
    public class LogSingle
    {
        private static readonly object Lock = new object();
        private static Log _instance;
        public static Log Instaince
        {
            get
            {
                if (_instance == null)
                {
                    lock (Lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Log();
                        }
                    }
                }
                return _instance;
            }
        }
    }
    #endregion
    public class Log
    {
        public AddMainLogDelegate WriteMainLogThing { get; set; }
        public OrderStateDelegate WriteOrderStateThing { get; set; }

        /// <summary>
        /// 写入主窗体日志
        /// </summary>
        /// <param name="Data"></param>
        public void WriteMainLog(string Data)
        {
            Task task = new Task(() =>
            {
                int NumberTime=0;
                    try
                    {
                        Thread.Sleep(150);
                        if (WriteMainLogThing != null)
                        {
                            WriteMainLogThing(Data);
                            LogWriter.Instaince.WriteLog("Log", Data);
                          
                        }
                    }
                    catch (Exception)
                    {
                        LogWriter.Instaince.WriteLog("Log", "记录失败");
                    }
                    NumberTime++;
  
            });
            task.Start();
        }

        /// <summary>
        /// 写入订单窗体状态
        /// </summary>
        /// <param name="State"></param>
        public void WriteOrderState(int State)
        {
            Task task = new Task(() =>
            {
                int NumberTime = 0;
                    try
                    {
                        Thread.Sleep(150);
                        if (WriteOrderStateThing != null)
                        {
                            WriteOrderStateThing(State);
                            LogWriter.Instaince.WriteLog("Log", "State:" + State);
                        }
                    }
                    catch
                    {
                        LogWriter.Instaince.WriteLog("Log", "State:"+ "状态错误");
                    }
                    NumberTime++;
            });
            task.Start();
        }
    }
}
